#coding=utf-8
#Boa:FramePanel:PanelExpense

import wx

from product import *
from MyListCtrl import MyListCtrlExpense

[wxID_PANELEXPENSE, wxID_PANELEXPENSEBTNALLDATE, wxID_PANELEXPENSEBTNDAYAFTER, 
 wxID_PANELEXPENSEBTNDAYBEFORE, wxID_PANELEXPENSEBTNNEW, 
 wxID_PANELEXPENSEBTNONEDAY, wxID_PANELEXPENSEBTNONEMONTH, 
 wxID_PANELEXPENSEBTNONEWEEK, wxID_PANELEXPENSEBTNONEYEAR, 
 wxID_PANELEXPENSEBTNRECYCLE, wxID_PANELEXPENSEBTNSEARCH, 
 wxID_PANELEXPENSECBQUERYDATE, wxID_PANELEXPENSECBSUBJECT, 
 wxID_PANELEXPENSEDATEEND, wxID_PANELEXPENSEDATESTART, 
 wxID_PANELEXPENSESTATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(16)]

class PanelExpense(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELEXPENSE, name=u'PanelExpense',
              parent=prnt, pos=wx.Point(0, 0), size=wx.Size(1051, 703),
              style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(1043, 669))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELEXPENSEDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(150, 30),
              size=wx.Size(120, 27), style=wx.DP_DROPDOWN)
        self.dateStart.SetLabel(u'2014-04-24')
        self.dateStart.SetValue(wx.DateTimeFromDMY(24, 3, 2014, 0, 0, 0))
        self.dateStart.SetToolTipString(u'dateStart')

        self.staticText1 = wx.StaticText(id=wxID_PANELEXPENSESTATICTEXT1,
              label=u'\u81f3', name='staticText1', parent=self,
              pos=wx.Point(278, 38), size=wx.Size(18, 18), style=0)

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELEXPENSEDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(302, 30),
              size=wx.Size(116, 27), style=wx.DP_DROPDOWN)

        self.btnOneDay = wx.Button(id=wxID_PANELEXPENSEBTNONEDAY,
              label=u'\u5f53\u65e5', name=u'btnOneDay', parent=self,
              pos=wx.Point(434, 30), size=wx.Size(46, 29), style=0)
        self.btnOneDay.Bind(wx.EVT_BUTTON, self.OnBtnOneDayButton,
              id=wxID_PANELEXPENSEBTNONEDAY)

        self.btnOneWeek = wx.Button(id=wxID_PANELEXPENSEBTNONEWEEK,
              label=u'\u4e00\u5468', name=u'btnOneWeek', parent=self,
              pos=wx.Point(482, 30), size=wx.Size(46, 29), style=0)
        self.btnOneWeek.Bind(wx.EVT_BUTTON, self.OnBtnOneWeekButton,
              id=wxID_PANELEXPENSEBTNONEWEEK)

        self.btnOneMonth = wx.Button(id=wxID_PANELEXPENSEBTNONEMONTH,
              label=u'\u4e00\u6708', name=u'btnOneMonth', parent=self,
              pos=wx.Point(530, 30), size=wx.Size(46, 29), style=0)
        self.btnOneMonth.Bind(wx.EVT_BUTTON, self.OnBtnOneMonthButton,
              id=wxID_PANELEXPENSEBTNONEMONTH)

        self.btnOneYear = wx.Button(id=wxID_PANELEXPENSEBTNONEYEAR,
              label=u'\u4e00\u5e74', name=u'btnOneYear', parent=self,
              pos=wx.Point(578, 30), size=wx.Size(46, 29), style=0)
        self.btnOneYear.Bind(wx.EVT_BUTTON, self.OnBtnOneYearButton,
              id=wxID_PANELEXPENSEBTNONEYEAR)

        self.btnAllDate = wx.Button(id=wxID_PANELEXPENSEBTNALLDATE,
              label=u'\u5168\u90e8', name=u'btnAllDate', parent=self,
              pos=wx.Point(626, 30), size=wx.Size(46, 29), style=0)
        self.btnAllDate.Bind(wx.EVT_BUTTON, self.OnBtnAllDateButton,
              id=wxID_PANELEXPENSEBTNALLDATE)

        self.btnDayBefore = wx.Button(id=wxID_PANELEXPENSEBTNDAYBEFORE,
              label=u'\u524d\u4e00\u5929', name=u'btnDayBefore', parent=self,
              pos=wx.Point(704, 30), size=wx.Size(64, 29), style=0)
        self.btnDayBefore.Bind(wx.EVT_BUTTON, self.OnBtnDayBeforeButton,
              id=wxID_PANELEXPENSEBTNDAYBEFORE)

        self.btnDayAfter = wx.Button(id=wxID_PANELEXPENSEBTNDAYAFTER,
              label=u'\u540e\u4e00\u5929', name=u'btnDayAfter', parent=self,
              pos=wx.Point(768, 30), size=wx.Size(64, 29), style=0)
        self.btnDayAfter.Bind(wx.EVT_BUTTON, self.OnBtnDayAfterButton,
              id=wxID_PANELEXPENSEBTNDAYAFTER)

        self.btnNew = wx.Button(id=wxID_PANELEXPENSEBTNNEW,
              label=u'\u65b0\u589e\u5f00\u652f', name=u'btnNew', parent=self,
              pos=wx.Point(24, 72), size=wx.Size(104, 29), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_PANELEXPENSEBTNNEW)

        self.btnSearch = wx.Button(id=wxID_PANELEXPENSEBTNSEARCH,
              label=u'\u67e5\u8be2', name=u'btnSearch', parent=self,
              pos=wx.Point(312, 72), size=wx.Size(86, 29), style=0)
        self.btnSearch.Bind(wx.EVT_BUTTON, self.OnBtnSearchButton,
              id=wxID_PANELEXPENSEBTNSEARCH)

        self.cbSubject = wx.ComboBox(choices=[], id=wxID_PANELEXPENSECBSUBJECT,
              name=u'cbSubject', parent=self, pos=wx.Point(152, 72),
              size=wx.Size(145, 27), style=0, value=u'')
        self.cbSubject.SetLabel(u'')

        self.btnRecycle = wx.Button(id=wxID_PANELEXPENSEBTNRECYCLE,
              label=u'\u67e5\u770b\u56de\u6536\u7ad9', name=u'btnRecycle',
              parent=self, pos=wx.Point(900, 72), size=wx.Size(104, 29),
              style=0)
        self.btnRecycle.Bind(wx.EVT_BUTTON, self.OnBtnRecycleButton,
              id=wxID_PANELEXPENSEBTNRECYCLE)

        self.cbQueryDate = wx.ComboBox(choices=[],
              id=wxID_PANELEXPENSECBQUERYDATE, name=u'cbQueryDate', parent=self,
              pos=wx.Point(24, 30), size=wx.Size(112, 27), style=0,
              value=u'\u66f4\u65b0\u65e5\u671f')
        self.cbQueryDate.SetLabel(u'\u66f4\u65b0\u65e5\u671f')

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()
        
    def myInit(self):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())
        
        self.lc = MyListCtrlExpense(self)
        self.lc.SetSize(wx.Size(1000, 450))
        self.lc.Move(wx.Point(22,110))
        
        for id,str in CDataAccountSubject.fetchSubjectList(income=0):
            self.cbSubject.Append(str, id)
        
        dateType = {'update_time':u'更新日期', 
                    'date':u'支出日期',
                    'create_time':u'创建日期'}
        for type,name in dateType.items():
            self.cbQueryDate.Append(name, type)
        self.cbQueryDate.SetStringSelection(u'更新日期')
            
        #self.lc.refresh()

    def OnBtnOneDayButton(self, event):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

    def OnBtnOneWeekButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Days(7)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        

    def OnBtnOneMonthButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Months(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnOneYearButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Years(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnAllDateButton(self, event):
        self.dateEnd.SetValue(wx.DateTime.Today())        
        self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))

    def OnBtnDayBeforeButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.SubtractDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)

    def OnBtnDayAfterButton(self, event):
        day = self.dateEnd.GetValue()        
        span = wx.DateSpan.Days(1)
        day.AddDS(span)
        self.dateEnd.SetValue(day)
        self.dateStart.SetValue(day)

    def OnBtnNewButton(self, event):
        import DlgExpense
        
        dlg = DlgExpense.DlgExpense(self)
        if dlg.ShowModal() == wx.ID_OK:
            pass

    def doClose(self, doQuit=True):
        return True

    def OnBtnSearchButton(self, event):
        filter = {'dateStart':self.dateStart.GetValue().Format('%Y-%m-%d')+' 00:00:00',
                    'dateEnd':self.dateEnd.GetValue().Format('%Y-%m-%d')+' 23:59:59'}
    
        if self.cbSubject.GetValue():
            filter['subject'] = self.cbSubject.GetClientData(self.cbSubject.GetSelection())
        
        filter['queryDate'] = self.cbQueryDate.GetClientData(self.cbQueryDate.GetSelection())   
         
        self.lc.refresh(filter)

    def OnBtnRecycleButton(self, event):
        self.lc.refresh({'delete':1})
